const LOGIN_COOKIE_NAME = 'JSESSIONID';

function _getCookie (name) {
  let start, end;
  if (document.cookie.length > 0) {
    start = document.cookie.indexOf(name + '=')
    if (start !== -1) {
      start = start + name.length + 1;
      end = document.cookie.indexOf(';', start)
      if (end === -1) {
        end = document.cookie.length;
      }
      return unescape(document.cookie.substring(start, end));
    }
  }
  return '';
}

function _setCookie (name, value, expire) {
  let date = new Date();
  // date.setDate(date.getDate() + expire);
  date.setTime(date.getTime() + expire)
  document.cookie = name + '=' + escape(JSON.stringify(value)) + '; path=/' +
    (expire ? ';expires=' + date.toUTCString() : '');
}

export function isAuthenticated () {
  return _getCookie(LOGIN_COOKIE_NAME);
};

export function authenticateSuccess (token) {
  _setCookie(LOGIN_COOKIE_NAME, token, 7200000);
};

export function logoutUser () {
  _setCookie(LOGIN_COOKIE_NAME, '', -10);
};

